/*
 * Copyright (c) 2025 Industrial Software Feature Database
 */
package com.comac.ins.isfd.strategy;


import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Component;

@Component
public class FastenerNasmDashCodeGenerationStrategy implements FastenerCodeGenerationStrategy {

    @Override
    public List<String> generateCodes(FastenerCodeContext context) {
        String fastenerGrade = context.getFastenerGrade();
        Double dashNumber = context.getDashNumber();
        List<String> result = new ArrayList<>();
        Integer[] dashRange = getDiameterByDashNumber(dashNumber);
        if (dashRange != null && dashRange.length > 0) {
            // 生成该范围内所有dash对应的编码
            for (Integer i : dashRange) {
                result.add(fastenerGrade + i);
            }
        }
        return result;
    }

    private Integer[] getDiameterByDashNumber(Double dash) {
        // 根据dash值返回对应的范围数组
        if (dash == 0.1640) {
            return getRangeArray(1, 35);
        } else if (dash == 0.188 || dash == 0.190) {
            return getRangeArray(46, 90);
        } else if (dash == 0.250) {
            return getRangeArray(92, 135);
        } else if (dash == 0.312) {
            return getRangeArray(137, 180);
        } else {
            // 不在任何范围内返回null
            return null;
        }
    }

    /**
     * 生成从start到end的连续整数数组
     */
    private Integer[] getRangeArray(int start, int end) {
        if (start > end) {
            return new Integer[0];
        }
        int length = end - start + 1;
        Integer[] array = new Integer[length];
        for (int i = 0; i < length; i++) {
            array[i] = start + i;
        }
        return array;
    }
}
